import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'

Vue.use(Router)

const  router = new Router({
  routes: [
    {
      path: '/',
      name: '/',
      component: () => import ('@/pages/account/login')
    },  {
      path: '/login',
      name: '/login',
      component: () => import ('@/pages/account/login')
    },
    {
      path: '/register',
      name: '注册页',
      component: () => import ('@/pages/account/register'),

    },
    {
      path: '/forget',
      name: '申请找回密码页',
      component: () => import ('@/pages/account/forget/forget'),

    },
    {
      path: '/forgettwo',
      name: '找回密码',
      component: () => import ('@/pages/account/forgettwo'),

    },
    /*首页*/
    {
      path: '/index',
      name: 'tabbar',
      redirect: {
        path: '/news',
      },
      component: () => import ('@/pages/home/index'),
      children:[
        {
          path: '/news',
          name: '消息',
          component:() => import ('@/pages/news/index'),
        },  {
          path: '/news/detail',
          name: '消息详情页',
          component:() => import ('@/pages/news/detail'),
        }, {
          path: '/work',
          name: '工作',
          component:() => import ('@/pages/work/index'),
        }, {
          path: '/work/backOffice/index',
          name: '考勤打卡',
          component:() => import ('@/pages/work/backOffice/index'),
        },  {
          path: '/statistics',
          name: '考勤统计',
          component:() => import ('@/pages/work/backOffice/statistics'),
        }, {
          path: '/day',
          name: '日报',
          component:() => import ('@/pages/work/report/day'),
        }, {
          path: '/week',
          name: '周报',
          component:() => import ('@/pages/work/report/week'),
        }, {
          path: '/month',
          name: '月报',
          component:() => import ('@/pages/work/report/month'),
        }, {
          path: '/goods',
          name: '月报',
          component:() => import ('@/pages/work/administration/goods'),
        },  {
          path: '/goods',
          name: '物品领用',
          component:() => import ('@/pages/work/administration/goods'),
        },  {
          path: '/car',
          name: '用车',
          component:() => import ('@/pages/work/administration/car'),
        },  {
          path: '/chapter',
          name: '用章',
          component:() => import ('@/pages/work/administration/chapter'),
        }, {
          path: '/empLeave',
          name: '请假',
          component:() => import ('@/pages/work/vacation/empLeave'),
        }, {
          path: '/workOvertime',
          name: '加班',
          component:() => import ('@/pages/work/vacation/workOvertime'),
        }, {
          path: '/business',
          name: '出差',
          component:() => import ('@/pages/work/vacation/business'),
        }, {
          path: '/finance',
          name: '费用报销',
          component:() => import ('@/pages/work/finance/index'),
        },  {
          path: '/personalCenter',
          name: '个人中心',
          component:() => import ('@/pages/personalCenter/index'),
        }
      ]
    },
  ]
});
// 挂载路由导航守卫
router.beforeEach((to, from, next) => {
  // to 将要访问的路径
  // from 代表从哪个路径跳转而来
  // next 是一个函数，表示放行
  //     next()  放行    next('/login')  强制跳转

  if (to.path === '/login' ||to.path === '/' ||to.path === '/register' ) return next()
  // 获取token
  const userLogin = window.localStorage.getItem('user')
  if (!userLogin) return next('/login')
  // console.log("用户信息1----"+ userLogin)
  next()
})
/*router.beforeEach((to,from,next)=>{
  const userLogin= localStorage.getItem('user')
  console.log("用户信息1----"+ userLogin)

  if(userLogin === null ){
    //如果有   /  -->  /home
   next()
  }else if (to.name === 'login' || to.name === '/'){
    if (userLogin == null){
      next('/')
    }else {
      next('/index');
    }
  }
  else {
    console.log("2232")
    next('/');
  }
})*/

export default router;
